package net.huashitong.ssydt.provider.pk.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.pk.entity.SsPkYmdxMyRank;
import net.huashitong.ssydt.provider.pk.mapper.SsPkYmdxMyRankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.13
 */
@Service
@Transactional
public class SsPkYmdxMyRankServiceImpl extends AbstractLongService<SsPkYmdxMyRank> implements SsPkYmdxMyRankService {

    private SsPkYmdxMyRankMapper mapper;

    private SsPkYmdxMatchRecordService ssPkYmdxMatchRecordService;

    @Autowired
    public SsPkYmdxMyRankServiceImpl(SsPkYmdxMyRankMapper mapper,
                                     SsPkYmdxMatchRecordService ssPkYmdxMatchRecordService) {
        this.mapper = mapper;
        this.ssPkYmdxMatchRecordService = ssPkYmdxMatchRecordService;
    }

    @Override
    protected LongMapper<SsPkYmdxMyRank> getLongMapper() {
        return this.mapper;
    }

    @Override
    public SsPkYmdxMyRank getAutoUpdateRankByMatchRecordIdWithUserId(Long matchRecordId, String userId) {
        SsPkYmdxMyRank ssPkYmdxMyRank = this.getAutoLoadLastRankByMatchRecordIdWithUserId(matchRecordId, userId);
        this.updateRank(ssPkYmdxMyRank);
        return ssPkYmdxMyRank;
    }

    @Override
    public SsPkYmdxMyRank getAutoLoadLastRankByMatchRecordIdWithUserId(Long matchRecordId, String userId) {
        SsPkYmdxMyRank ssPkYmdxMyRank = this.getByMatchRecordIdWithUserId(matchRecordId, userId);
        if (ssPkYmdxMyRank == null) {
            int lastRanking = 0;
            BigDecimal lastHighestScore = BigDecimal.ZERO;
            Long lastMatchRecordId = ssPkYmdxMatchRecordService.getLastMatchRecordId(matchRecordId);
            if (lastMatchRecordId != null && lastMatchRecordId != 0L) {
                SsPkYmdxMyRank lastObj = this.getByMatchRecordIdWithUserId(lastMatchRecordId, userId);
                if (lastObj != null) {
                    lastObj = this.updateRank(lastObj);
                    lastRanking = lastObj.getRanking();
                    lastHighestScore = lastObj.getHighestScore();
                }
            }

            ssPkYmdxMyRank = new SsPkYmdxMyRank();
            ssPkYmdxMyRank.setMatchRecordId(matchRecordId);
            ssPkYmdxMyRank.setUserId(userId);
            ssPkYmdxMyRank.setRanking(0);
            ssPkYmdxMyRank.setHighestScore(BigDecimal.ZERO);
            ssPkYmdxMyRank.setHighestGmtModified(new Date(System.currentTimeMillis()));
            ssPkYmdxMyRank.setLastRanking(lastRanking);
            ssPkYmdxMyRank.setLastHighestScore(lastHighestScore);
            this.mapper.insertSelective(ssPkYmdxMyRank);
        }
        return ssPkYmdxMyRank;
    }

    /**
     * 更新本轮排行
     *
     * @param ssPkYmdxMyRank
     * @return 更新排行之后的对象
     */
    public SsPkYmdxMyRank updateRank(SsPkYmdxMyRank ssPkYmdxMyRank) {
        int ranking = this.mapper.getRankByHighestScoreDescAndHighestGmtModifiedAsc(ssPkYmdxMyRank);
        if (ranking > 1000) {
            ranking = 0;
        }
        ssPkYmdxMyRank.setRanking(ranking);
        this.updateByPrimaryKeySelective(ssPkYmdxMyRank);
        return ssPkYmdxMyRank;
    }

    @Override
    public SsPkYmdxMyRank getByMatchRecordIdWithUserId(Long matchRecordId, String userId) {
        SsPkYmdxMyRank ssPkYmdxMyRank = new SsPkYmdxMyRank();
        ssPkYmdxMyRank.setMatchRecordId(matchRecordId);
        ssPkYmdxMyRank.setUserId(userId);
        return this.mapper.getByMatchRecordIdWithUserId(ssPkYmdxMyRank);
    }

    @Override
    public List<SsPkYmdxMyRank> findHighestScoreDescByMatchRecordId(Long matchRecordId) {
        return this.mapper.findHighestScoreDescByMatchRecordId(matchRecordId);
    }


}
